$(function()
{
	//panel初始化
	$('#mailPanel').panel({
		border:false
	});
	
	//panel初始化
	$('#addressBookPanel').panel({
		border:false
	});
	
	//发送邮件
	$('#sendMailBtn').linkbutton({plain:true, iconCls:'icon-mail-send'});
	//关闭
	$('#closeWriteMailBtn').linkbutton({plain:true,iconCls:'icon-close'});
	
	//初始化通讯录按钮
	$('#addressBookBtn').linkbutton({plain:true, iconCls:'icon-address-book'});
	$('#abOkBtn').linkbutton({plain:true, iconCls:'icon-ok'});
	$('#abCloseBtn').linkbutton({plain:true, iconCls:'icon-close'});
	
	
	//初始化通讯录窗口
	$('#addressBookWindow').window({
		title:'通讯录',
		closed:true,
		modal:true,
		closable:true,
		minimizable:false,
		maximizable:false,
		collapsible:false,
		resizable:false,
		iconCls:'icon-address-book',
		width:300,
		height:400
	});	
	
	//初始化通讯录TREE
	$('#addressBookTree').tree( {
		checkbox:true,
		url:'departAjax!loadFullDeparts',
		loadMsg:'数据加载中......',
		border:false,
		onDblClick: function(node){
			$(this).tree('toggle',node.id);
		}
	});
	
	//初始化文本编辑器
	$('#mailContent').xheditor({width:'100%',height:'400px',tools:'Blocktag,Fontface,FontSize,|,Bold,Italic,Underline,|,FontColor,BackColor,|,Align,List,|,Link,Hr,Table',skin:'o2007blue'});
	$('#mailContent').val('dddddddddddddddddddddddddddd');
});



//通讯录选择完毕
function addressBookOk(){
	var nodes = $('#addressBookTree').tree('getChecked');
	var receiveUserName = '';
	var receiveUserIds = '';
	for(var i=0; i<nodes.length; i++){		
		if(nodes[i].id.substr(0, 1)=='p'){
			//接收人
			if(receiveUserName != '') receiveUserName += ',';
			receiveUserName += nodes[i].text;
			//接收人ID组
			if (receiveUserIds != '') receiveUserIds += ',';
			var nodeId = nodes[i].id;
			receiveUserIds += nodeId.substr(1, nodeId.length);
		}		
	}
	$('#mailReceiveNames').val(receiveUserName);
	$('#mailReceiveIds').val(receiveUserIds);

	closeAddressBookWindow();
}

function openAddressBookWindow(){
	$('#addressBookWindow').window('open');
}

function closeAddressBookWindow(){
	$('#addressBookWindow').window('close');
}

function sendMail(){
	var mailTitle = $('#mailTitle').val();
	var mailReceiveIds = $('#mailReceiveIds').val();
	var mailContent = $('#mailContent').val();
	
	if(mailTitle == ''){
		$.messager.alert('提示','请填写主题!');
		return;
	}
	
	if(mailReceiveIds == ''){
		$.messager.alert('提示','请选择收件人!');
		return;
	}
	
/*	var mailAtts = '';
	
	$("input[name='upload']").each(function(){
		if(mailAtts != ''){
			mailAtts += '|';
		}
		mailAtts = mailAtts + $(this).val();
	});*/

	//, mailAtts: mailAtts
	$.post('mailAjax!sendMail', {mailTitle:mailTitle, receiveIds: mailReceiveIds, mailContent: mailContent},		    		
			function(data){
				$.messager.confirm('提示', '邮件已发送成功,需要再写一封邮件吗?', function(r){
					if(r){
						$('#mailTitle').val('');
						$('#mailReceiveNames').val('');
						$('#mailReceiveIds').val('');
						$('#mailContent').val('');
					}else{
		 				curTabClose();	
		 			}
		 		});
	        }, 'json');
	
}